package xworker;

import com.gluonhq.attach.storage.StorageService;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;
import org.xmeta.util.ExceptionUtil;

import java.io.File;
import java.util.Optional;

public class JavaFXApplication  extends Application {
    @Override
    public void start(Stage stage) throws Exception {

        Parent root = null;
        try {
            File rootFile;
            Optional<StorageService> storageService = StorageService.create();
            if (storageService.isPresent()) {
                try {
                    Optional<File> rootFileOp = storageService.get().getPrivateStorage();
                    rootFile = rootFileOp.orElseGet(() -> new File("./"));
                } catch (Exception ignored) {
                    rootFile = new File("./");
                }
            } else {
                rootFile = new File("./");
            }

            StringBuilder sb = new StringBuilder();
            if (rootFile.exists()) {
                World world = World.getInstance();
                world.init(rootFile.getPath() + "/xworker/");

                String path = "xworker.javafx.examples.JavaFXMobileExamples/@Nodes/@rootVBox";
                Thing app = world.getThing(path);

                if (app != null) {
                    JavaActionRegister.regist();
                    BuilderFactoryRegister.regist();

                    root = app.doAction("create", new ActionContext());
                }else{
                    sb.append("Thing not exists, ").append(path);
                }
            }else{
                sb.append("Root file not exists, ").append(rootFile);
            }

            if(root == null){
                VBox vBox = new VBox();
                TextArea textArea = new TextArea();
                textArea.setText(sb.toString());
                VBox.setVgrow(textArea, Priority.ALWAYS);
                vBox.getChildren().add(textArea);
                Button exitButton = new Button("Exit");
                exitButton.setOnAction(event -> System.exit(0));
                vBox.getChildren().add(exitButton);
                root = vBox;
            }
        }catch(Exception e){
            VBox vBox = new VBox();
            TextArea textArea = new TextArea();
            textArea.setText(ExceptionUtil.toString(e));
            VBox.setVgrow(textArea, Priority.ALWAYS);
            vBox.getChildren().add(textArea);
            Button exitButton = new Button("Exit");
            exitButton.setOnAction(event -> System.exit(0));
            vBox.getChildren().add(exitButton);
            root = vBox;
        }
        Scene scene = new Scene(root, 1024, 768);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args){
        launch(args);
    }
}
